using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Webadmin_Modules_User_GroupEdit : System.Web.UI.UserControl
{
    //----------------------------------------------------------
    public DataRow _SiteOption = null;
    public DataRow _AdminLang = null;   
    public string _Act = "";
    private string _SiteNote = "General";
    private string _LangNote = "UserGroup";
    public string _Filter = "";
    public string _Navigator = "";
    public string _ID = "";
    public string _CateID = "";
    private void initSiteLang()
    {
       
        if (!Request.Url.AbsoluteUri.Contains(Session["Page"].ToString()))
        {
            Session["Page"] = Request.Url.AbsoluteUri.Split('?')[0];
        }
        //---------
        Session["Lang"] = Request.QueryString["Lang"] == null ? Session["Lang"] : Request.QueryString["Lang"];
        Session["Lang"] = bzAdminLang.checkLang(Session["Lang"].ToString());
        _SiteOption = new bzSiteOption("SiteAdmin", _SiteNote).SiteOption;
        _AdminLang = new bzAdminLang(Session["Lang"].ToString(), _LangNote).AdminLang;

        this.check(bzCheckAdminUser.Type);
        _Act = Request.QueryString["Act"] == null ? "" : Request.QueryString["Act"];
        _ID = Request.QueryString["ID"] == null ? "" : Request.QueryString["ID"];
        _CateID = Request.QueryString["CateID"] == null ? "" : Request.QueryString["CateID"];

        this.loadAction();
    }
    private bool check(int Per)
    {
        if (bzCheckAdminUser.checkUserPermission(Session["AdminUser"].ToString(), Session["AdminLevel"].ToString(), Session["IP"].ToString(), Session["LoginID"].ToString(), Session["GroupUser"].ToString(), Session["Page"].ToString(), Per)) return true;
        Response.Redirect(bzStringClass.AlertPermission);
        return false;
    }
    //----------------------------------------------------------------------------------



    private DataTable _dt = new DataTable();
    private bzUserGroup bz = new bzUserGroup();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.initSiteLang();
    } 
    private void loadAction()
    {
        if (!IsPostBack)
        {
            switch (_Act)
            {
                case "Edit": this.loadEdit(); break;
                case "Add": this.resetData(); break;
            }            
        }
    }
    private void loadEdit()
    {
        _Navigator = _AdminLang["MenuUser"].ToString() + _AdminLang["GeneralArrow"].ToString() + _AdminLang["MenuUserGroup"].ToString() + _AdminLang["GeneralArrow"].ToString() + _AdminLang["Edit"].ToString();
        this.txtGroupUser.ReadOnly = true;
        this.btnUpdate.Text = _AdminLang["Edit"].ToString();
        
        _dt = bz.getRecord(_ID);
        DataRow dr=_dt.Rows[0];
        this.txtGroupUser.Text = dr["GroupUser"].ToString();
        this.rdForward_1.Enabled = false;
        this.rdForward_0.Checked = true;
        this.radType_Admin.Checked = Convert.ToInt32(dr["AdminLevel"]) <= 0 ? false : true;
        this.radType_User.Checked = !this.radType_Admin.Checked;
        this.rdEnabled_1.Checked = Convert.ToInt16(dr["Enabled"]) <= 0 ? false : true;
        this.rdEnabled_0.Checked = !this.rdEnabled_1.Checked;
    }
    
    private void resetData()
    {
        _Navigator = _AdminLang["MenuUser"].ToString() + _AdminLang["GeneralArrow"].ToString() + _AdminLang["MenuUserGroup"].ToString() + _AdminLang["GeneralArrow"].ToString() + _AdminLang["AddNew"].ToString();
        this.txtGroupUser.Text = "";
        this.radType_User.Checked = true;
        this.radType_Admin.Checked = !this.radType_User.Checked;

        this.rdForward_0.Checked = false;
        this.rdForward_1.Checked = !this.rdForward_0.Checked;
        this.btnUpdate.Text = _AdminLang["AddNew"].ToString();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        switch (_Act)
        {
            case "Edit": this.doEdit(); break;
            case "Add": this.doAdd(); break;
        }   
    }
    private void doEdit()
    {
        if (this.check(bzCheckAdminUser.Edit))
        {
            if (bz.updateRecord(this.txtGroupUser.Text, this.radType_Admin.Checked ? "1" : "0", this.rdEnabled_0.Checked ? "0" : "1") > 0)
            {
                Response.Write(bzStringClass.addStringAlert(_AdminLang["Success"]));
                Response.Redirect(Session["Page"].ToString());
            }
        }
    }
    private void doAdd()
    {
        if (this.check(bzCheckAdminUser.Type))
        {
            if (bz.insertRecord(this.txtGroupUser.Text, this.radType_Admin.Checked ? "1" : "0", this.rdEnabled_0.Checked ? "0" : "1") > 0)
            {
                if (this.rdForward_0.Checked)
                    Response.Redirect(Session["Page"].ToString());
                else this.resetData();
            }
        }
    }
}
